---
layout: "default"
title: "ManagedBufferPointer"
description: "Swift documentation for 'ManagedBufferPointer': Contains a buffer object, and provides access to an instance of."
keywords: "ManagedBufferPointer,struct,swift,documentation,holdsUniqueOrPinnedReference,holdsUniqueReference,withUnsafeMutablePointerToElements,withUnsafeMutablePointerToValue,withUnsafeMutablePointers,allocatedElementCount,buffer,value"
root: "/v1.2"
---

<div class="intro-declaration"><code class="language-swift">struct ManagedBufferPointer&lt;Value, Element&gt;</code></div>

<div class="discussion comment">
    <p>Contains a buffer object, and provides access to an instance of
<code>Value</code> and contiguous storage for an arbitrary number of
<code>Element</code> instances stored in that buffer.</p>

<p>For most purposes, the <code>ManagedBuffer</code> class works fine for this
purpose, and can simply be used on its own.  However, in cases
where objects of various different classes must serve as storage,
<code>ManagedBufferPointer</code> is needed.</p>

<p>A valid buffer class is non-<code>@objc</code>, with no declared stored
  properties.  Its <code>deinit</code> must destroy its
  stored <code>Value</code> and any constructed <code>Element</code>s.</p>

<h2>Example Buffer Class</h2>

<pre><code class="language-swift"> class MyBuffer&lt;Element&gt; { // non-@objc
   typealias Manager = ManagedBufferPointer&lt;(Int,String), Element&gt;
   deinit {
     Manager(unsafeBufferObject: self).withUnsafeMutablePointers {
       (pointerToValue, pointerToElements)-&gt;Void in
       pointerToElements.destroy(self.count)
       pointerToValue.destroy()
     }
   }

   // All properties are *computed* based on members of the Value
   var count: Int {
     return Manager(unsafeBufferObject: self).value.0
   }
   var name: String {
     return Manager(unsafeBufferObject: self).value.1
   }
 }</code></pre>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">Equatable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init-bufferclass_minimumcapacity_initialvalue_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-bufferclass_minimumcapacity_initialvalue_">init(<wbr>bufferClass:<wbr>minimumCapacity:<wbr>initialValue:)</a><div class="comment collapse" id="comment-init-bufferclass_minimumcapacity_initialvalue_"><div class="p">
    <p>Create with new storage containing an initial <code>Value</code> and space
for at least <code>minimumCapacity</code> <code>element</code>s.</p>

<p><strong><code>bufferClass</code></strong> the class of the object used for storage.
<strong><code>minimumCapacity</code></strong> the minimum number of <code>Element</code>s that
  must be able to be stored in the new buffer.
<strong><code>initialValue</code></strong> a function that produces the initial
  <code>Value</code> instance stored in the buffer, given the <code>buffer</code>
  object and a function that can be called on it to get the actual
  number of allocated elements.</p>

<p>Requires: minimumCapacity &gt;= 0, and the type indicated by
<code>bufferClass</code> is a non-<code>@objc</code> class with no declared stored
properties.  The <code>deinit</code> of <code>bufferClass</code> must destroy its
stored <code>Value</code> and any constructed <code>Element</code>s.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(bufferClass: AnyClass, minimumCapacity: Int, initialValue: (buffer: AnyObject, allocatedCount: (AnyObject) -&gt; Int) -&gt; Value)</code>

    </div></div>
</div>
<div class="declaration" id="init-unsafebufferobject_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-unsafebufferobject_">init(<wbr>unsafeBufferObject:)</a><div class="comment collapse" id="comment-init-unsafebufferobject_"><div class="p">
    <p>Manage the given <code>buffer</code>.</p>

<p>Requires: <code>buffer</code> is an instance of a non-<code>@objc</code> class whose
<code>deinit</code> destroys its stored <code>Value</code> and any constructed
<code>Element</code>s.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(unsafeBufferObject buffer: AnyObject)</code>

    </div></div>
</div>


<h3>Instance Variables</h3>
<div class="declaration" id="var-allocatedelementcount_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-allocatedelementcount_-int">var allocatedElementCount: Int</a><div class="comment collapse" id="comment-var-allocatedelementcount_-int"><div class="p">
    <p>The actual number of elements that can be stored in this object.</p>

<p>This value may be nontrivial to compute; it is usually a good
idea to store this information in the &quot;value&quot; area when
an instance is created.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var allocatedElementCount: Int { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-buffer_-anyobject">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-buffer_-anyobject">var buffer: AnyObject</a><div class="comment collapse" id="comment-var-buffer_-anyobject"><div class="p">
    <p>Return the object instance being used for storage.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var buffer: AnyObject { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-value_-value">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-value_-value">var value: Value</a><div class="comment collapse" id="comment-var-value_-value"><div class="p">
    <p>The stored <code>Value</code> instance.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var value: Value { get set }</code>

    </div></div>
</div>



<h3>Instance Methods</h3>
<div class="declaration" id="func-holdsuniqueorpinnedreference">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-holdsuniqueorpinnedreference">mutating func holdsUniqueOrPinnedReference()</a>
        
<div class="comment collapse" id="comment-func-holdsuniqueorpinnedreference"><div class="p">
    <p>Returns true iff either <code>self</code> holds the only strong reference
to its buffer or the pinned has been &#39;pinned&#39;.</p>

<p>See <code>isUniquelyReferenced</code> for details.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func holdsUniqueOrPinnedReference() -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-holdsuniquereference">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-holdsuniquereference">mutating func holdsUniqueReference()</a>
        
<div class="comment collapse" id="comment-func-holdsuniquereference"><div class="p">
    <p>Returns true iff <code>self</code> holds the only strong reference to its buffer.</p>

<p>See <code>isUniquelyReferenced</code> for details.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func holdsUniqueReference() -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-withunsafemutablepointertoelements_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-withunsafemutablepointertoelements_">func withUnsafeMutablePointerToElements(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-withunsafemutablepointertoelements_"><div class="p">
    <p>Call <code>body</code> with an <code>UnsafeMutablePointer</code> to the <code>Element</code>
storage.  <strong>Note</strong>: this pointer is only valid
for the duration of the call to <code>body</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func withUnsafeMutablePointerToElements&lt;R&gt;(body: (UnsafeMutablePointer&lt;Element&gt;) -&gt; R) -&gt; R</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-withunsafemutablepointertovalue_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-withunsafemutablepointertovalue_">func withUnsafeMutablePointerToValue(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-withunsafemutablepointertovalue_"><div class="p">
    <p>Call <code>body</code> with an <code>UnsafeMutablePointer</code> to the stored
<code>Value</code>.  <strong>Note</strong>: this pointer is only valid
for the duration of the call to <code>body</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func withUnsafeMutablePointerToValue&lt;R&gt;(body: (UnsafeMutablePointer&lt;Value&gt;) -&gt; R) -&gt; R</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-withunsafemutablepointers_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-withunsafemutablepointers_">func withUnsafeMutablePointers(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-withunsafemutablepointers_"><div class="p">
    <p>Call <code>body</code> with <code>UnsafeMutablePointer</code>s to the stored <code>Value</code>
and raw <code>Element</code> storage.  <strong>Note</strong>: these pointers are only valid
for the duration of the call to <code>body</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func withUnsafeMutablePointers&lt;R&gt;(body: (UnsafeMutablePointer&lt;Value&gt;, UnsafeMutablePointer&lt;Element&gt;) -&gt; R) -&gt; R</code>
    
    
</div></div>
</div>


